package com.zr.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.zr.dao.SQCXDaoWW;
import com.zr.model.Employee;
import com.zr.model.User;
import com.zr.util.JDBCUtil;

public class SQCXDaoImplWW  implements  SQCXDaoWW {

	@Override
	public int CountUserByEid(int eid) {
		int i = 0;
		StringBuffer sql = new StringBuffer("");
		sql.append(" select COUNT(eid) as cs FROM overtime ");
		sql.append(" where overtime.eid=? ");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, eid);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				i=rs.getInt("cs");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}


	@Override
	public List<Employee> selUserByEid(int eid, int page, int size) {
		List<Employee> user = new ArrayList<Employee>();
		StringBuffer sql = new StringBuffer("");
		sql.append(" select employee.ename,department.dname,overtime.wcontent,overtime.opinion,overtime.remake,overtime.retime  ");
		sql.append(" from overtime ");
		sql.append(" INNER JOIN employee ");
		sql.append(" ON employee.eid = overtime.eid ");
		sql.append(" INNER JOIN department ");
		sql.append(" ON department.did = employee.did ");
		sql.append(" WHERE overtime.eid =? ");
		sql.append(" limit ?,? ");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, eid);
			pst.setInt(2, (page-1)*size);
			pst.setInt(3, size);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Employee emp = new Employee();
				emp.setEname(rs.getString("ename"));
				emp.setDepartment(rs.getString("dname"));
				emp.setWcontent(rs.getString("wcontent"));
				emp.setOpinion(rs.getString("opinion"));
				emp.setRemake(rs.getString("remake"));
				emp.setRetime(rs.getString("retime"));
				user.add(emp);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return user;
	}
}